package com.shop.manager.controller;

//import com.shop.manager.entity.Areas;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Areas;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.AreasService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *  @Description 区域信息增删改查
 */

@RestController
@RequestMapping("/areas")
public class AreasController {

    /**
     * @Description 注入service对象
     */
    @Resource(type = AreasService.class)
    private AreasService areasService;

    /**
     * @Description 添加区域信息
     * @param areas 区域对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "Area 对象", required = true) Areas areas) {
        try {
            areasService.add(areas);
            return HttpResult.ok("添加区域成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加地址失败!");
    }

    /**
     * @Description 批量删除区域信息
     * @param ids 区域id的数组
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HttpResult delete(@RequestBody Integer[] ids) {
        try {
            areasService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改区域信息
     * @param areas 修改的区域对象
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Area 对象") Areas areas) {
        try {
            areasService.update(areas);
            return HttpResult.ok("修改区域成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改区域失败!");
    }

    /**
     * @Description 根据id查询区域信息
     * @param id 区域id
     * @return
     */
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public HttpResult getById(@RequestParam("id") Integer id) {
        return HttpResult.ok(areasService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param areas 查询条件对象
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) @ApiParam(value = "分页条件Area对象") Areas areas) {
        try {
            //分页查询
            PageResult result = areasService.findPage(pageNum, size, areas);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询区域异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = areasService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询区域信息
     * @param areas 扩展属性对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "Areas 对象") Areas areas) {
        return HttpResult.ok(areasService.findByWhere(areas));
    }


}
